package zj.com.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import zj.com.thead.BingLoad;
import zj.com.thead.Load;
import zj.com.utils.LocalDownJpg;

import java.util.Objects;

/**
 * 图片爬取工具
 * 2021年7月30日 11:19:19
 * TODO
 */
public class HttpClientCrawler {
//    public static Integer listNum=1;
    //获取百度的数据
    public static void main(String[] args) throws Exception {
        String [] sArr={"水泥搅拌车"};

//     getBaidu(sArr);//获取百度
     getBing(sArr);//获取bing

    }

    /**
     * 获取bing的数据
     * @throws Exception
     */
    public static void getBaidu(String [] sArr) throws Exception{
//        String [] sArr={"罐车","水平定向钻","地勘机"};
//       String [] sArr={"罐车"};
        for (String s:sArr ){
            new Thread(new Load(s,1000)).start();
        }
    }
    //通过ajax 获取的数据
    public static void getUrl(Integer page,String word) throws Exception{
//        String word="罐车";
        Integer limit=10;
        String url="https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord="+word+"&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&word="+word+"&face=0&istype=2&nc=1&pn="+page*30+"&rn=30";
        CloseableHttpClient build = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("User-Agent","Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0");
        httpGet.addHeader("Upgrade-Insecure-Requests","1");
        CloseableHttpResponse execute = build.execute(httpGet);
        HttpEntity entity = execute.getEntity();
        String str = EntityUtils.toString(entity, "utf-8");
        System.out.println(url);
        JSONObject jsonObject = JSON.parseObject(str);
        JSONArray data = jsonObject.getJSONArray("data");
        for (Object o:data) {
            JSONObject o1 = (JSONObject) o;
            boolean aNull = Objects.isNull(o1);
            if (!aNull&&o1.size()!=0){
                String thumbURL = o1.get("thumbURL").toString();
                String cs = o1.get("cs").toString();
                String[] split = cs.split(",");
                String s = split[0] + split[1];
                System.out.println(thumbURL);
                LocalDownJpg.download(thumbURL,"百度/"+word,s);
            }
        }
//         listNum = Integer.valueOf(jsonObject.get("listNum").toString());
    }

    /**
     * 获取bing的数据
     * @throws Exception
     */
    public static void getBing(String [] sArr) throws Exception{
//        String [] sArr={"罐车","水平定向钻机","地勘机"};
//       String [] sArr={"罐车"};
        for (String s:sArr ){
            new Thread(new BingLoad(s,1500)).start();
        }
    }

    public  static void  getXml(Integer page,String word) throws  Exception{
//        word="罐车";
        Integer count=35;
        String url="https://cn.bing.com/images/async?q="+word+"&first="+page*count+"&count="+count+"&cw=1177&ch=1040&relp=35&tsc=ImageBasicHover&datsrc=I&layout=RowBased_Landscape&mmasync=1&dgState=x*257_y*1157_h*184_c*1_i*106_r*24&IG=61EA22C3552F4106AF2FC508724B6A00&SFX=4&iid=images.5551";
        CloseableHttpClient build = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
        httpGet.addHeader("Connection","keep-alive");
        httpGet.addHeader("User-Agent","Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0");
        httpGet.addHeader("Upgrade-Insecure-Requests","1");
        CloseableHttpResponse execute = build.execute(httpGet);
        HttpEntity entity = execute.getEntity();
        String str = EntityUtils.toString(entity, "utf-8");
        Document parse = Jsoup.parse(str);
//        System.out.println(parse);
//        Elements linksElements = parse.select("div#main>div#content>article#post>header.entry-header>h3.entry-title>a");
        Elements linksElements = parse.select("div.img_cont>img.mimg");
        for (Element ele:linksElements) {
            String href = ele.attr("src");
            String[] split = href.split("\\?");
            String[] split1 = split[0].split("/");
            String s = split1[split1.length - 1];
            LocalDownJpg.download(split[0],"bing/"+word,s);
        }
    }

}
